/************************************************************************************/
/*  Program name CAPAREXT_CUSTOMERS.sql			                                    */
/*  Creater:  CHAN KA KEUNG BILLY (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 09, MAY 2006 CHAN KA KEUNG BILLY     First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAR_CUSTOMERS';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPAR_CUSTOMERS.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT	* 
	FROM  RA_CUSTOMERS
	WHERE SUBSTR(CUSTOMER_NUMBER,1,4) = lv_NGO;
	
BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.ROW_ID                         ||CHR(9)|| 
				C1.CUSTOMER_ID                    ||CHR(9)|| 
				C1.PARTY_ID                       ||CHR(9)|| 
				C1.PARTY_NUMBER                   ||CHR(9)|| 
				C1.PARTY_TYPE                     ||CHR(9)|| 
				C1.LAST_UPDATE_DATE               ||CHR(9)|| 
				C1.LAST_UPDATED_BY                ||CHR(9)|| 
				C1.CREATION_DATE                  ||CHR(9)|| 
				C1.CREATED_BY                     ||CHR(9)|| 
				C1.CUSTOMER_NAME                  ||CHR(9)|| 
				C1.CUSTOMER_NUMBER                ||CHR(9)|| 
				C1.ORIG_SYSTEM_REFERENCE          ||CHR(9)|| 
				C1.STATUS                         ||CHR(9)|| 
				C1.LAST_UPDATE_LOGIN              ||CHR(9)|| 
				C1.CUSTOMER_TYPE                  ||CHR(9)|| 
				C1.CUSTOMER_PROSPECT_CODE         ||CHR(9)|| 
				C1.CUSTOMER_CLASS_CODE            ||CHR(9)|| 
				C1.PRIMARY_SALESREP_ID            ||CHR(9)|| 
				C1.SALES_CHANNEL_CODE             ||CHR(9)|| 
				C1.SIC_CODE                       ||CHR(9)|| 
				C1.ORDER_TYPE_ID                  ||CHR(9)|| 
				C1.PRICE_LIST_ID                  ||CHR(9)|| 
				C1.ATTRIBUTE_CATEGORY             ||CHR(9)|| 
				C1.ATTRIBUTE1                     ||CHR(9)|| 
				C1.ATTRIBUTE2                     ||CHR(9)|| 
				C1.ATTRIBUTE3                     ||CHR(9)|| 
				C1.ATTRIBUTE4                     ||CHR(9)|| 
				C1.ATTRIBUTE5                     ||CHR(9)|| 
				C1.ATTRIBUTE6                     ||CHR(9)|| 
				C1.ATTRIBUTE7                     ||CHR(9)|| 
				C1.ATTRIBUTE8                     ||CHR(9)|| 
				C1.ATTRIBUTE9                     ||CHR(9)|| 
				C1.ATTRIBUTE10                    ||CHR(9)|| 
				C1.REQUEST_ID                     ||CHR(9)|| 
				C1.PROGRAM_APPLICATION_ID         ||CHR(9)|| 
				C1.PROGRAM_ID                     ||CHR(9)|| 
				C1.PROGRAM_UPDATE_DATE            ||CHR(9)|| 
				C1.ANALYSIS_FY                    ||CHR(9)|| 
				C1.CUSTOMER_CATEGORY_CODE         ||CHR(9)|| 
				C1.CUSTOMER_GROUP_CODE            ||CHR(9)|| 
				C1.CUSTOMER_KEY                   ||CHR(9)|| 
				C1.CUSTOMER_SUBGROUP_CODE         ||CHR(9)|| 
				C1.FISCAL_YEAREND_MONTH           ||CHR(9)|| 
				C1.NET_WORTH                      ||CHR(9)|| 
				C1.NUM_OF_EMPLOYEES               ||CHR(9)|| 
				C1.POTENTIAL_REVENUE_CURR_FY      ||CHR(9)|| 
				C1.POTENTIAL_REVENUE_NEXT_FY      ||CHR(9)|| 
				C1.RANK                           ||CHR(9)|| 
				C1.REFERENCE_USE_FLAG             ||CHR(9)|| 
				C1.TAX_CODE                       ||CHR(9)|| 
				C1.TAX_REFERENCE                  ||CHR(9)|| 
				C1.ATTRIBUTE11                    ||CHR(9)|| 
				C1.ATTRIBUTE12                    ||CHR(9)|| 
				C1.ATTRIBUTE13                    ||CHR(9)|| 
				C1.ATTRIBUTE14                    ||CHR(9)|| 
				C1.ATTRIBUTE15                    ||CHR(9)|| 
				C1.THIRD_PARTY_FLAG               ||CHR(9)|| 
				C1.ACCESS_TEMPLATE_ENTITY_CODE    ||CHR(9)|| 
				C1.PRIMARY_SPECIALIST_ID          ||CHR(9)|| 
				C1.SECONDARY_SPECIALIST_ID        ||CHR(9)|| 
				C1.COMPETITOR_FLAG                ||CHR(9)|| 
				C1.DUNNING_SITE_USE_ID            ||CHR(9)|| 
				C1.STATEMENT_SITE_USE_ID          ||CHR(9)|| 
				C1.ORIG_SYSTEM                    ||CHR(9)|| 
				C1.YEAR_ESTABLISHED               ||CHR(9)|| 
				C1.COTERMINATE_DAY_MONTH          ||CHR(9)|| 
				C1.FOB_POINT                      ||CHR(9)|| 
				C1.FREIGHT_TERM                   ||CHR(9)|| 
				C1.GSA_INDICATOR                  ||CHR(9)|| 
				C1.SHIP_PARTIAL                   ||CHR(9)|| 
				C1.SHIP_VIA                       ||CHR(9)|| 
				C1.WAREHOUSE_ID                   ||CHR(9)|| 
				C1.PAYMENT_TERM_ID                ||CHR(9)|| 
				C1.TAX_EXEMPT                     ||CHR(9)|| 
				C1.TAX_EXEMPT_NUM                 ||CHR(9)|| 
				C1.TAX_EXEMPT_REASON_CODE         ||CHR(9)|| 
				C1.JGZZ_FISCAL_CODE               ||CHR(9)|| 
				C1.DO_NOT_MAIL_FLAG               ||CHR(9)|| 
				C1.MISSION_STATEMENT              ||CHR(9)|| 
				C1.CUSTOMER_NAME_PHONETIC         ||CHR(9)|| 
				C1.TAX_HEADER_LEVEL_FLAG          ||CHR(9)|| 
				C1.TAX_ROUNDING_RULE              ||CHR(9)|| 
				C1.WH_UPDATE_DATE                 ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE1              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE2              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE3              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE4              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE5              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE6              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE7              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE8              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE9              ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE10             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE11             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE12             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE13             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE14             ||CHR(9)||  
				C1.GLOBAL_ATTRIBUTE15             ||CHR(9)||  
				C1.GLOBAL_ATTRIBUTE16             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE17             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE18             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE19             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE20             ||CHR(9)|| 
				C1.GLOBAL_ATTRIBUTE_CATEGORY      ||CHR(9)|| 
				C1.URL                            ||CHR(9)|| 
				C1.LANGUAGE                       ||CHR(9)|| 
				C1.TRANSLATED_CUSTOMER_NAME       ||CHR(9)|| 
				C1.PERSON_PRE_NAME_ADJUNCT        ||CHR(9)|| 
				C1.PERSON_FIRST_NAME              ||CHR(9)|| 
				C1.PERSON_MIDDLE_NAME             ||CHR(9)|| 
				C1.PERSON_LAST_NAME               ||CHR(9)|| 
				C1.PERSON_SUFFIX                  ||CHR(9)|| 
				C1.PERSON_FIRST_NAME_PHONETIC     ||CHR(9)|| 
				C1.PERSON_LAST_NAME_PHONETIC      ||CHR(9)|| 
				C1.SHIP_SETS_INCLUDE_LINES_FLAG   ||CHR(9)|| 
				C1.ARRIVALSETS_INCLUDE_LINES_FLAG ||CHR(9)|| 
				C1.SCHED_DATE_PUSH_FLAG           ||CHR(9)|| 
				C1.OVER_SHIPMENT_TOLERANCE        ||CHR(9)|| 
				C1.UNDER_SHIPMENT_TOLERANCE       ||CHR(9)|| 
				C1.OVER_RETURN_TOLERANCE          ||CHR(9)|| 
				C1.UNDER_RETURN_TOLERANCE         ||CHR(9)|| 
				C1.ITEM_CROSS_REF_PREF            ||CHR(9)|| 
				C1.DATE_TYPE_PREFERENCE           ||CHR(9)|| 
				C1.DATES_NEGATIVE_TOLERANCE       ||CHR(9)|| 
				C1.DATES_POSITIVE_TOLERANCE       ||CHR(9)|| 
				C1.INVOICE_QUANTITY_RULE          ||CHR(9)|| 
				C1.ATTRIBUTE16                    ||CHR(9)|| 
				C1.ATTRIBUTE17                    ||CHR(9)|| 
				C1.ATTRIBUTE18                    ||CHR(9)|| 
				C1.ATTRIBUTE19                    ||CHR(9)|| 
				C1.ATTRIBUTE20                    ||CHR(9)|| 
				C1.DUNS_NUMBER                    ||CHR(9)|| 
				C1.DUNS_NUMBER_C                  ||CHR(9)|| 
				C1.PARTY_LAST_UPDATE_DATE         ||CHR(9)|| 
				C1.SIC_CODE_TYPE                   
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Concurrent Program for this NGO';
		END IF;
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/